<template>
	<el-container class="home_container">
		<!-- 头部区域 -->
	  <el-header>
		  <div>
			  <img src="../assets/sheng.png" alt="">
			  <span>后台管理系统</span>
		  </div>
		  <el-button type="info" @click="logout">退出登陆</el-button>
	  </el-header>
	  <!-- 页面主体区域 -->
	  <el-container>
		  <!-- 左侧菜单区域 -->
	    <el-aside :width="isCollapse?'64px':'200px'">
			<div class="toggle-button" @click="toggleCollapse">|||</div>
				<!-- 侧边栏菜单区域 -->
		 <el-menu
			  background-color="#222831"
			  text-color="#ececec"
			  active-text-color="#ffd04b"
			  :unique-opened="true"
			  :collapse="isCollapse"  
			  :collapse-transition="false"  
			  :router="true"
			  :default-active="activePath"
			  >
			  <!-- 一级菜单 -->
			  <el-submenu index="item.id+''"
				v-for='item in menuList'
				:index="item.id+''"
				:key="item.id"
				>
				<template slot="title">
				  <i :class="iconList[item.id]"></i>
				  <span>{{item.authName}}</span>
				</template>
				<!-- 二级菜单 -->
				<el-menu-item 
					:index="'/'+subitem.path"
					v-for="subitem in item.children"
					:key="subitem.id"
					@click="saveNavState('/'+subitem.path)"
				>
					 <template slot="title">
					   <i class="el-icon-menu"></i>
					   <span>{{subitem.authName}}</span>
					 </template>
				</el-menu-item>
			  </el-submenu>
			</el-menu>
		</el-aside>
		<!-- 右侧主体区域 -->
	    <el-container>
			<!-- 主体文章内容区域-->
	      <el-main>
			  <!-- 创建路由占位符 -->
			  <router-view></router-view>
		  </el-main>
	    </el-container>
	  </el-container>
	</el-container>
</template>

<script>
	export default{
		data(){
			return{
				menuList:[],
				iconList:{
					'125':'iconfont icon-add-account',
					'103':'iconfont icon-unlock-fill',
					'101':'iconfont icon-goumaipianhao',
					'102':'iconfont icon-wuliudingdan',
					'145':'iconfont icon-tradingdata'
				},
				isCollapse:false,
				activePath:''
			}
		},
		methods:{
			//退出登陆
			logout(){
				//第一步：销毁sessionStorage中的token值
				window.sessionStorage.clear()
				//第二步：编程式导航跳转到登陆界面
				this.$router.push('/login')
				this.$message.success('退出成功')
			},
			//获取后台的数据-左侧菜单栏
			async getMenuList(){
				const{data:res}=await this.$http.get('menus')
				this.menuList=res.data
			},
			//点击切换左侧是否收起
			toggleCollapse(){
				this.isCollapse=!this.isCollapse
			},
			//把选中的菜单的状态保存起来
			saveNavState(path){
				window.sessionStorage.setItem('activePath',path)
				this.activePath=path
			}
		},
		//利用vue的生命周期函数
		created() {
			this.getMenuList()
			//从sessionStorage中把保存的激活菜单的地址取出来
			this.activePath=window.sessionStorage.getItem('activePath')
		}
	}
</script>

<style lang="stylus" scoped>
	.el-container >>> .el-menu
		border none
	.home_container
		height 100%
		.el-header
			background-color: #373d41;
			display: flex;
			justify-content: space-between;
			padding-left: 0;
			align-items: center;
			color: #fff;
			font-size: 20px;
			padding 0 20px
			div
				display flex
				align-items center
				span
					margin-left 15px
		.el-aside
			background-color: #333744;
			.toggle-button
				background-color: #4A5064;
				font-size: 10px;
				line-height: 24px;
				text-align: center;
				cursor: pointer;
				color: #fff;
				letter-spacing: 0.2em;
		.el-main
			background-color: #eaedf1;
</style>
